package com.hairline.hairlinemarket.service.impl;

import com.hairline.hairlinemarket.bean.MarketBrand;
import com.hairline.hairlinemarket.bean.MarketBrandExample;
import com.hairline.hairlinemarket.bean.MarketGoods;
import com.hairline.hairlinemarket.bean.MarketGoodsExample;
import com.hairline.hairlinemarket.mapper.MarketBrandMapper;
import com.hairline.hairlinemarket.mapper.MarketGoodsMapper;
import com.hairline.hairlinemarket.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:杨小凯
 * Date:2023-04-20 18:46
 * Description:<描述>
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private MarketBrandMapper marketBrandMapper;
    @Autowired
    private MarketGoodsMapper marketGoodsMapper;

    @Override
    public List query(int start, Integer brandLimit) {
        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = brandExample.createCriteria();
        // TODO 排序
        brandExample.setOrderByClause("add_time");
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(brandExample);
        ArrayList<MarketBrand> list = new ArrayList<>();
        for (int i = 0; i < brandLimit; i++) {
            list.add(marketBrands.get(i));
        }
        return list;
    }

    @Override
    public List queryById(Integer id) {
        // 查出brandId
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        Integer brandId = marketGoods.get(0).getBrandId();
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andIdEqualTo(brandId);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        return marketBrands;
    }
}
